<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>

    /*
     一个方法，该方法将对象转换为相应的原始 值。
     由 ToPrimitive 抽象操作使用
     */
    class Foo {
    }

    let foo = new Foo();
    console.log(3 + foo);// "3[object Object]"
    console.log(3 - foo);// NaN
    console.log(String(foo));// "[object Object]"

    class Bar {
        constructor() {
            this[Symbol.toPrimitive] = function (hint) {
                switch (hint) {
                    case 'number':
                        return 3;
                    case 'string':
                        return 'string bar';
                    case 'default':
                    default:
                        return 'default bar';
                }
            }
        }
    }

    let bar = new Bar();
    console.log(3 + bar); // "3default bar"
    console.log(3 - bar); // 0
    console.log(String(bar)); // "string bar"
</script>
</body>
</html>